//
//  FourthDetailViewController.m
//  A段项目
//
//  Created by RAMBO on 15/4/16.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FourthDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "DetailCollectionViewCell.h"
#import "FourthDetailCollectionViewCell.h"

#define WIDTH self.view.frame.size.width
#define X 30
#define F 0
#define BACKGROUNDCOLOR [UIColor colorWithRed:F green:F blue:F alpha:F]
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface FourthDetailViewController () <UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *myCollectionView;
@end

@implementation FourthDetailViewController

- (void)dealloc
{
    [_detailArray release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self makeSelf];
}

#pragma mark -
#pragma mark 创建界面
- (void)makeSelf
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UICollectionViewFlowLayout *flowOut = [[UICollectionViewFlowLayout alloc] init];
    flowOut.itemSize = CGSizeMake(WIDTH - 10, HEIGHT - 64 - 42);
    [flowOut setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -15, self.view.frame.size.width , HEIGHT - 64 + 30) collectionViewLayout:flowOut];
    [self.view addSubview:_myCollectionView];
    [_myCollectionView release];
    [flowOut release];
    // 这个地方存放努力加载的图片 (有待更改的地方)
    [_myCollectionView setBackgroundColor:[UIColor whiteColor]];
    
    [_myCollectionView setDelegate:self];
    [_myCollectionView setDataSource:self];
    [_myCollectionView registerClass:[FourthDetailCollectionViewCell class] forCellWithReuseIdentifier:@"collect"];
    _myCollectionView.autoresizesSubviews = YES;
    _myCollectionView.pagingEnabled = YES;
    _myCollectionView.showsHorizontalScrollIndicator = NO;
    
    NSIndexPath *dex = [NSIndexPath indexPathForItem:self.click inSection:0];
    
    // 指定滑动到第一个cell
    [_myCollectionView scrollToItemAtIndexPath:dex atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}

#pragma mark -
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSInteger cou = [_detailArray count];
    return cou - 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FourthDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collect" forIndexPath:indexPath];
    
    NSDictionary *dic = [self.detailArray objectAtIndex:indexPath.item];
    NSString *link_video = [dic objectForKey:@"link_video"];
    NSRange range = [link_video rangeOfString:@"mp4"];
    link_video = [NSString stringWithFormat:@"%@", [link_video substringToIndex:range.location + range.length]];
    NSURL *url = [NSURL URLWithString:link_video];
    [cell.imageScroll loadRequest:[NSURLRequest requestWithURL:url]];
    
    [cell.source setText:[dic objectForKey:@"source"]];
    [cell.titleLabel setText:[dic objectForKey:@"title"]];
    [cell.authorLabel setText:[dic objectForKey:@"author"]];
    
    NSString *content = [dic objectForKey:@"content"];
    [cell.contentWebView loadHTMLString:content baseURL:nil];
    
    return cell;
    
}

@end
